#!/usr/bin/python
# -*- coding:utf-8 -*-

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import preprocessing 
import heapq

# step 4 补充数值
def paddingData(T, listA, listU, listL, realA, pos):
    for i in range(1,T):
        if listA[i][pos] == 0 and listA[i-1][pos] != 0:
            listA[i][pos] = listA[i-1][pos]
        if listU[i][pos] == 0 and listU[i-1][pos] != 0:
            listU[i][pos] = listU[i-1][pos]
        if listL[i][pos] == 0 and listL[i-1][pos] != 0:
            listL[i][pos] = listL[i-1][pos]
        if realA[i][pos] == 0 and realA[i-1][pos] != 0:
            realA[i][pos] = realA[i-1][pos]

    peopleAbility = [i[pos] for i in listA]
    UpperBound = [i[pos]*0.0003 for i in listU]
    LowerBound = [i[pos]*0.0003 for i in listL]
    peopleRealA = [i[pos] for i in realA]
    return peopleAbility, UpperBound, LowerBound, peopleRealA


# step 5 画图
def plotStu(T, peopleAbility, UpperBound, LowerBound, peopleRealA):
    font1 = {'family' : 'Times New Roman',
            'weight' : 'normal',
            'size'   : 11
    }
    font2 = {'family' : 'Times New Roman',
            'weight' : 'bold',
            'size'   : 11
    }

    plt.plot(range(1,T+1), peopleAbility, '*', label='Ability', linewidth=1, markersize=5)
    # plt.plot(range(1,T+1), UpperBound, linestyle='--', label='UpperBound', linewidth=1)
    # plt.plot(range(1,T+1), LowerBound, linestyle='-.', label='LowerBound', linewidth=1)
    plt.xticks(fontproperties = 'Times New Roman', size = 5)
    plt.yticks(fontproperties = 'Times New Roman', size = 5)
    plt.xlabel("time", font2)
    plt.ylabel("people ability", font2)
    plt.legend(prop=font1)
    plt.savefig("top1.png", dpi=600)

    # realA
    plt.figure()
    plt.plot(range(1,T+1), peopleRealA, '.', label='real ability', linewidth=1, markersize=5)
    plt.xticks(fontproperties = 'Times New Roman', size = 5)
    plt.yticks(fontproperties = 'Times New Roman', size = 5)
    plt.xlabel("time", font2)
    plt.ylabel("people real ability", font2)
    plt.legend(prop=font1)
    plt.savefig("real ability.png", dpi=600)





# step 1 读取数据
dataA = pd.read_csv("tmpdataA.csv", header=None, skiprows=[0])
# listA = np.array(dataA)
listA = dataA.values[:, 1:].tolist()
dataU = pd.read_csv("tmpdataU.csv", header=None, skiprows=[0])
listU = dataU.values[:, 1:].tolist()
dataL = pd.read_csv("tmpdataL.csv", header=None, skiprows=[0])
listL = dataL.values[:, 1:].tolist()
realA = pd.read_csv("realA.csv", header=None, skiprows=[0])
realA = realA.values[:, 1:].tolist()
T = 60

# step 2 数据归一化
# npA = np.array(realA)
# min_max_scaler = preprocessing.MinMaxScaler() 
# realA = min_max_scaler.fit_transform(npA)
# # print(listA)
# npU = np.array(listU)
# listU = min_max_scaler.fit_transform(npU)
# # print(listU)
# npL = np.array(listL)
# nmlzL = min_max_scaler.fit_transform(npL)
# # print(nmlzL)

# step 3 找出出现次数最多的四个人
tmpTime = np.zeros(shape=(6800,1))
for j in range(0,6800):
    peopleAbility = [i[j] for i in listA]
    for tmp in peopleAbility:
        if tmp != 0:
            tmpTime[j] += 1


ind = heapq.nlargest(3, range(len(tmpTime)), tmpTime.__getitem__) # 下标
print(ind)
pos = ind[0]
print(heapq.nlargest(3,tmpTime))

peopleAbility, UpperBound, LowerBound, peopleRealA = paddingData(T, listA, listU, listL, realA, ind[0])
plotStu(T, peopleAbility, UpperBound, LowerBound, peopleRealA)

